//
//  Networktools.swift
//  SinaBlog
//
//  Created by macs on 15/11/16.
//  Copyright © 2015年 BlackTail. All rights reserved.
//

import UIKit
import AFNetworking

let blogDomain = "com.baidu.data.error"

//swift 中的枚举
enum HTTPType : String {
    
    case POST = "POST"
    case GET = "GET"
    
}
//创建网络分类,隔离 AFN框架与 自身代码

class Networktools: AFHTTPSessionManager {
    //定义单例对象,设置属性
    static let shareTools : Networktools = {
        
        let baseUrl = "https://api.weibo.com/"
        let tools = Networktools(baseURL: NSURL(string: baseUrl))
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    } ()
    
    func requestJsonDict( type : HTTPType , urlSting : String, parameters: [String : AnyObject]?,finished : (result : [String : AnyObject]? , error : NSError? )->() ) {
        
        if type == HTTPType.GET {
            
            GET(urlSting, parameters: parameters, success: { (_, result) -> Void in
                //判断是否是字典
                if let dict = result as? [String : AnyObject] {
                    
                    finished(result: dict, error: nil)
                    return
                }
                
                //如果不是字典,返回自定义错误
                let dataError = NSError(domain: blogDomain, code: -10000, userInfo: [NSLocalizedDescriptionKey : "数据错误"])
            
                finished(result: nil, error: dataError)
                
                }, failure: { (_, error) -> Void in
                    finished(result: nil, error: error)
                    print(error)
            })
            
        } else {
            
            POST(urlSting, parameters: parameters, success: { (_, result) -> Void in
                //判断是否是字典
                if let dict = result as? [String : AnyObject] {
                    
                    finished(result: dict, error: nil)
                    return
                }
                
                //如果不是字典,返回自定义错误
                let dataError = NSError(domain: blogDomain, code: -10000, userInfo: [NSLocalizedDescriptionKey : "数据错误"])
                finished(result: nil, error: dataError)
                
                }, failure: { (_, error) -> Void in
                    finished(result: nil, error: error)
                    print(error)
            })
            
        }
    }
    //上传图片的方法
    
    func uploadPicture(urlSting : String, parameters: [String : AnyObject]?,imageData : NSData,finished : (result : [String : AnyObject]? , error : NSError? )->() ){
        
        POST(urlSting, parameters: parameters, constructingBodyWithBlock: { (multipartFormData) -> Void in
            /**
            *  imageData,图片的二进制数据
                name : 上传到服务器的对应接口名
                fileName : 服务器接收到存储的名字
                mimeType : 上传文件的格式
            */
            multipartFormData.appendPartWithFileData(imageData, name: "pic", fileName: "OMG", mimeType: "image/jpeg")
            
            }, success: { (_, result) -> Void in
                //成功会掉
                if let dict = result as? [String : AnyObject] {
                    
                    finished(result: dict, error: nil)
                }
            }) { (_, error) -> Void in
                
                //失败回调
                finished(result: nil, error: error)
                
        }
        
    }
    
    
    
}
